/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.util.ModelerConstants;

/**
 * @author zl
 * 
 */
public class GenSampleActions extends AbstractGenerator {
	private IFolder folder;

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.pku.sei.modeler.jet.codegen.AbstractGenerator#generateCode(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void generateCode(IProgressMonitor monitor)
			throws InterruptedException {
		assert proj != null;
		// internal, startup class
		String codeBase = ModelerConstants.pkgBase + "."
				+ ModelerConstants.internalBase;

		folder = proj.getFolder("src/" + codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create internal folder failed");
		}
		genStartupClass(monitor);

		// actions, action & action provider
		codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.actionBase;

		folder = proj.getFolder("src/" + codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create action folder failed");
		}
		genActions(monitor);
		genActionProvider(monitor);
	}

	public void genStartupClass(IProgressMonitor monitor)
			throws InterruptedException {
		try {
			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/SampleStartUp.javajet";

			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri,
					Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");

			String result = emitter.generate(monitor,
					new Object[] { new Object() });
			IFile javaFile = folder.getFile(ModelerConstants.starupName
					+ ".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result
					.getBytes());
			javaFile.create(is, true, monitor);
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate startup class failed");
		}
	}

	public void genActions(IProgressMonitor monitor)
			throws InterruptedException {
		try {
			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/actions/SampleAction.javajet";

			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri,
					Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");

			String result = emitter.generate(monitor,
					new Object[] { new Object() });
			IFile javaFile = folder.getFile("SampleAction.java");
			ByteArrayInputStream is = new ByteArrayInputStream(result
					.getBytes());
			javaFile.create(is, true, monitor);
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate action provider failed");
		}
	}

	public void genActionProvider(IProgressMonitor monitor)
			throws InterruptedException {
		try {
			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/actions/SampleActionProvider.javajet";

			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri,
					Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");

			String result = emitter.generate(monitor,
					new Object[] { new Object() });
			IFile javaFile = folder.getFile(ModelerConstants.actionPrivider
					+ ".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result
					.getBytes());
			javaFile.create(is, true, monitor);
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate action provider failed");
		}
	}
}
